package de.slothsoft.wasbinich.android.database.local;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import de.slothsoft.wasbinich.core.FullPicture;
import de.slothsoft.wasbinich.core.database.DatabaseException;
import de.slothsoft.wasbinich.core.database.ManagerBean;

public class SqlManagerBean implements ManagerBean {

    private final SQLiteDatabase database;

    public SqlManagerBean(SqlDatabaseHelper databaseHelper) {
	this.database = databaseHelper.getWritableDatabase();
    }

    @Override
    public FullPicture createPicture(FullPicture picture) throws DatabaseException {
	ContentValues values = pictureToContentValues(picture);
	long insertId = this.database.insert(SqlPictureHelper.TABLE_NAME, null, values);
	return getPicture(insertId);
    }

    @Override
    public FullPicture updatePicture(FullPicture picture) throws DatabaseException {
	ContentValues values = pictureToContentValues(picture);
	this.database
		.update(SqlPictureHelper.TABLE_NAME, values, SqlPictureHelper.COLUMN_ID + " = " + picture.id, null); //$NON-NLS-1$
	return getPicture(picture.id);
    }

    @Override
    public FullPicture getPicture(long id) throws DatabaseException {
	Cursor cursor = this.database.query(SqlPictureHelper.TABLE_NAME, SqlPictureHelper.COLUMNS_FULL_PICTURE,
		SqlPictureHelper.COLUMN_ID + " = " + id, null, null, null, null); //$NON-NLS-1$
	cursor.moveToFirst();
	FullPicture result = cursorToPicture(cursor);
	cursor.close();
	return result;
    }

    @Override
    public List<FullPicture> getPictures() throws DatabaseException {
	List<FullPicture> pictures = new ArrayList<FullPicture>();

	Cursor cursor = this.database.query(SqlPictureHelper.TABLE_NAME, SqlPictureHelper.COLUMNS_FULL_PICTURE, null,
		null, null, null, null);

	cursor.moveToFirst();
	while (!cursor.isAfterLast()) {
	    FullPicture picture = cursorToPicture(cursor);
	    pictures.add(picture);
	    cursor.moveToNext();
	}
	cursor.close();
	return pictures;
    }

    @Override
    public void deletePicture(long id) {
	this.database.delete(SqlPictureHelper.TABLE_NAME, SqlPictureHelper.COLUMN_ID + " = " + id, null); //$NON-NLS-1$
    }

    public void deleteAll() {
	this.database.delete(SqlPictureHelper.TABLE_NAME, null, null);
    }

    private ContentValues pictureToContentValues(FullPicture picture) {
	ContentValues values = new ContentValues();
	values.put(SqlPictureHelper.COLUMN_SECTION, picture.section);
	values.put(SqlPictureHelper.COLUMN_BIG_PICTURE, picture.bigPicture);
	values.put(SqlPictureHelper.COLUMN_DESCRIPTION_DE, picture.description_de);
	values.put(SqlPictureHelper.COLUMN_DESCRIPTION_EN, picture.description_en);
	values.put(SqlPictureHelper.COLUMN_CORRECT_ANSWERS_DE, picture.correctAnswers_de);
	values.put(SqlPictureHelper.COLUMN_CORRECT_ANSWERS_EN, picture.correctAnswers_en);
	Log.d("slothsoft", "picture length: " + picture.section.length()); //$NON-NLS-1$ //$NON-NLS-2$
	return values;
    }

    private FullPicture cursorToPicture(Cursor cursor) {
	if (cursor.isAfterLast())
	    return null;
	int index = 0;
	return new FullPicture(cursor.getLong(index++), cursor.getString(index++), cursor.getString(index++),
		cursor.getString(index++), cursor.getString(index++), cursor.getString(index++),
		cursor.getString(index++));
    }

}